package com.dpwgc.demo.controller;

import com.dpwgc.demo.service.UploadService;
import com.dpwgc.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping(value = "/Upload")
public class UploadController {

    @Autowired
    UploadService uploadService;

    //存储根目录(静态资源文件夹)
    @Value("${sftp.rootPath}")
    String rootPath;

    /**
     * 上传文件
     * @param multipartFile 文件
     * @param url 上传路径
     * @param fileRename 文件命名
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST)
    public ResultUtil<Object> uploadFile(@RequestParam(value = "multipartFile") MultipartFile multipartFile,
                                         @RequestParam(value = "url") String url,
                                         @RequestParam(value = "fileRename") String fileRename,
                                         HttpSession session) throws Exception {
        if (multipartFile==null){
            ResultUtil<Object> result = new ResultUtil<>();
            result.setCode(100);
            result.setMsg("文件内容为空");
            return result;
        }

        return uploadService.uploadFileService(rootPath+url,multipartFile,fileRename);
    }
}
